package com.cndw.kungfu.model.disciple;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cndw.kungfu.config.ConstantsAct;
import com.cndw.kungfu.config.ConstantsSystem;
import com.cndw.kungfu.config.DiscipleConfig;
import com.cndw.kungfu.dao.DiscipleDao;
import com.cndw.kungfu.ext.NumberProcesser;
import com.cndw.kungfu.ext.PropertiesLib;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.disciple.domain.Disciple;
import com.cndw.kungfu.model.disciple.domain.DiscipleMode;
import com.cndw.kungfu.model.disciple.domain.DiscipleRein;

/**
 * 门派管理类
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-6-24 上午10:21:57
 */
@Component
public class DiscipleManager {
	@Autowired
	private DiscipleDao discipleDao;

	private int rantArray(int[] luckArr, int rateBase) {
		int rate = NumberProcesser.mtRand(rateBase);
		int cRate = 0;
		for (int i = 0; i < luckArr.length; i++) {
			cRate += luckArr[i];
			if (cRate >= rate) {
				return i;
			}
		}
		return 0;
	}

	public DiscipleRein makeRefresh(int reinType) {
		int[] qualityRate = DiscipleConfig.reinRate.get(reinType);
		int quality = rantArray(qualityRate, 10000);
		return makeRefresh(reinType, quality);
	}

	public DiscipleRein makeRefresh(int reinType, int quality) {
		return makeRefresh(reinType, quality, 0, true);
	}

	public DiscipleRein makeRefresh(int reinType, int quality, int spcDic, boolean rein) {
		int[] featureRate = DiscipleConfig.reinFeature.get(reinType);
		int featureLevel = rantArray(featureRate, 10000);
		int featureType = featureLevel > 0 ? NumberProcesser.mtRand(1, 8) : 0;
		int qualifiedType = NumberProcesser.mtRand(1, 4);// 资质类型
		if (rein && DiscipleConfig.openSpc() && quality == 4 && NumberProcesser.isWinning(20)) {
			spcDic = (NumberProcesser.isWinning(45) ? 100 : 200) + ConstantsAct.spcDiscipleSign;
			featureLevel = 4;
		}
		return makeRein(quality, qualifiedType, featureType, featureLevel, reinType, spcDic);
	}

	private DiscipleRein makeRein(int quality, int qualifiedType, int featureType, int featureLevel, int reinType, int spcDic) {
		DiscipleRein rein = new DiscipleRein();
		int discipleId = quality * 100 + qualifiedType + 100;
		DiscipleMode discipleMode = Context.getDiscipleServer().getDiscipleMode(discipleId);
		if (discipleMode == null) {
			discipleMode = Context.getDiscipleServer().getDiscipleMode(101);
		}

		String name = null;
		String icon = null;
		if (spcDic > 0) {
			int sex = spcDic / ConstantsAct.spcDiscipleBase;
			int indexId = spcDic % ConstantsAct.spcDiscipleBase;// 活动标记
			icon = ConstantsAct.spcDiscipleBase * sex + indexId + "";
			name = PropertiesLib.getCacheArr(PropertiesLib.nameXml, sex == 1 ? "discipleAct1" : "discipleAct2")[indexId];

		} else if (quality >= 3) {
			String nameKey = quality == 3 ? "discipleSpecial3" : "discipleSpecial4";
			String[] names = PropertiesLib.getCacheArr(PropertiesLib.nameXml, nameKey);
			int nameIndex = NumberProcesser.mtRand(names.length);
			name = names[nameIndex - 1];
			icon = 1000 * quality + nameIndex + "";
		} else {
			int sex = NumberProcesser.mtRand(100) < 50 ? 1 : 2;
			String surName = PropertiesLib.getArrayValue(PropertiesLib.nameXml, "discipleSur");
			String nameKey = sex == 1 ? "boyName" : "girlName";
			String bodyName = PropertiesLib.getArrayValue(PropertiesLib.nameXml, nameKey);

			name = surName + bodyName;
			icon = NumberProcesser.mtRand(18) + sex * 10000 + "";
		}

		rein.setName(name);
		rein.setIcon(icon);
		rein.setDiscipleId(discipleId);
		rein.setQuality(quality);
		rein.setQualifiedType(discipleMode.getQualifiedType());
		rein.setFeatureType(featureType);
		rein.setFeatureLevel(featureLevel);
		rein.setReinType(reinType);
		return rein;
	}

	public Disciple createDisciple(long playerId, DiscipleRein rein) {
		Disciple disciple = new Disciple();
		disciple.setPlayerId(playerId);
		disciple.setDiscipleId(rein.getDiscipleId());
		disciple.setName(rein.getName());
		disciple.setIcon(Integer.valueOf(rein.getIcon()));
		disciple.setQuality(rein.getQuality());
		disciple.setLevel(1);

		DiscipleMode discipleMode = disciple.getDiscipleMode();
		disciple.setGrowth(NumberProcesser.mtRand(discipleMode.getGrowthMin(), discipleMode.getReinGrowthMax()));

		disciple.setFeatureType(rein.getFeatureType());
		disciple.setFeatureLevel(rein.getFeatureLevel());

		rateDisciplePercent(disciple);

		disciple.setCreateTime(TimeProcesser.getUnixTime());

		return disciple;
	}

	public void rateDisciplePercent(Disciple disciple) {
		DiscipleMode discipleMode = disciple.getDiscipleMode();
		int[] floatArr = discipleMode.getQualifiedFloatArr();
		if (floatArr == null || floatArr.length < 8) {
			return;
		}
		disciple.setPowerPercent(NumberProcesser.mtRand(floatArr[0], floatArr[1]));
		disciple.setPhysicalPercent(NumberProcesser.mtRand(floatArr[2], floatArr[3]));
		disciple.setAgilePercent(NumberProcesser.mtRand(floatArr[4], floatArr[5]));
		disciple.setWitPercent(NumberProcesser.mtRand(floatArr[6], floatArr[7]));

		disciple.clacPropertys();
	}

	/**
	 * 获取我的徒弟
	 * 
	 * @param playerId
	 * @return
	 */
	public Map<Integer, Disciple> getDiscipleMapDB(long playerId) {
		try {
			return discipleDao.select(playerId);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public int insertDisciple(Disciple disciple) {
		try {
			return discipleDao.insert(disciple).intValue();
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}

	}

	public Map<Integer, DiscipleMode> getDiscipleModeMapDB() {
		try {
			return discipleDao.getDiscipleModeMap();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public boolean syncDiscipleToDB(Disciple disciple) {
		try {
			if (disciple.getPlayerId() == 0) {
				discipleDao.delete(disciple.getId());
			} else {
				discipleDao.update(disciple);
			}

		} catch (Exception e) {
			ConstantsSystem.throwExceptionDB(e);
			return false;
		}
		return true;
	}

	public boolean isSafe(int type) {
		return DiscipleConfig.reinRate.containsKey(type) && DiscipleConfig.reinFeature.containsKey(type);
	}

}
